//
//  KLGoodDetailHeaderView.m
//  KunLun
//
//  Created by Mac on 2017/11/28.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLGoodDetailHeaderView.h"
#import "STAutoAddView.h"
#import "STXingXIngView.h"
#import "KLClearTableHeaderView.h"
@interface KLGoodDetailHeaderView()
@property(nonatomic, strong) STLabel                     *titleLable;
@property(nonatomic, strong) STLabel                     *priceLable;
@property(nonatomic, strong) STLabel                     *originPriceLable;

@property(nonatomic, strong) STLabel                     *desLable;
@property(nonatomic, strong) STLabel                     *baoyouLable;
@property(nonatomic, strong) STLabel                     *ziyingLable;
@property(nonatomic, strong) STLabel                     *numLable;//月消和库存
@property(nonatomic, strong) STLabel                     *cuxiaoLable;//促销
@property(nonatomic, strong) UIView                      *cuxiaoView;//
@property(nonatomic, strong) UIView                      *bootomView;//

@property(nonatomic, strong) UIImageView                     *iconImageView;
@property(nonatomic, strong) STXingXIngView              *xingxingView;
@property(nonatomic, strong) STLabel                     *nameLable;//商品名字
@property(nonatomic, strong) STLabel                     *uploadLable;//是否缴纳保证金
@property(nonatomic, strong) STLabel                     *idLable;//商品名字
@property(nonatomic, strong) STLabel                     *goodDetailLable;//商品描述
@property(nonatomic, strong) UIView                     *graView;


@property(nonatomic, strong) KLClearTableHeaderView                     *clearView;
@end
@implementation KLGoodDetailHeaderView
- (void)setHitTestView:(UIView *)hitTestView{
    _hitTestView = hitTestView;
    self.clearView.hitTestView = hitTestView;
}
#pragma mark --subView
- (void)configSubView{
    self.backgroundColor = [UIColor clearColor];
    
    KLClearTableHeaderView * clearView = [[KLClearTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, KLIconHeaderHeight)];
    clearView.backgroundColor = [UIColor clearColor];
    self.clearView = clearView;
    [self addSubview:clearView];
    
    UIView * grayView = [[UIView alloc] initWithFrame:CGRectMake(0, clearView.st_bottom, UIScreenWidth, KLIconHeaderHeight)];
    grayView.backgroundColor = KL_backgroundColor;
    [self addSubview:grayView];
    
    //--------------------------第一部分
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, KLIconHeaderHeight)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [grayView addSubview:whiteView];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 0, UIScreenWidth - 20, 40)
                                                     text:@"-------"
                                                textColor:KL_firstTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    self.titleLable = titleLable;
    self.titleLable.numberOfLines = 0;//最多2行，不在适配
    [whiteView addSubview:self.titleLable];
    
    self.priceLable = [[STLabel alloc] initWithFrame:CGRectMake(10, self.titleLable.bottom+ 3, UIScreenWidth - 20, 18)
                                                text:@"折扣券"
                                           textColor:KL_redColor
                                                font:15
                                         isSizetoFit:NO
                                       textAlignment:NSTextAlignmentLeft];
    
    [whiteView addSubview:self.priceLable];
    
    self.originPriceLable = [[STLabel alloc] initWithFrame:CGRectMake(self.priceLable.st_right + 3, 0, 120, 18)
                                                      text:@"-----"
                                                 textColor:KL_secendTextColor
                                                      font:15
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentLeft];
    self.originPriceLable.centerY = self.priceLable.centerY;
    self.originPriceLable.hidden = YES;
    [whiteView addSubview:self.originPriceLable];
    
    STLabel * numLable = [[STLabel alloc] initWithFrame:CGRectMake(10, self.priceLable.bottom , 80, 30)
                                                   text:@"购买数量:"
                                              textColor:KL_secendTextColor
                                                   font:15
                                            isSizetoFit:NO
                                          textAlignment:NSTextAlignmentLeft];
    [whiteView addSubview:numLable];
    
    self.autoView = [[STAutoAddView alloc] initWithFrame:CGRectMake(numLable.right, self.priceLable.bottom, 90, 30)];
    [whiteView addSubview:self.autoView];
    

    self.numLable = [[STLabel alloc] initWithFrame:CGRectMake(10 , self.autoView.st_bottom + 15, UIScreenWidth  - 20, 15)
                                              text:@"--------"
                                         textColor:KL_secendTextColor
                                              font:13
                                       isSizetoFit:NO
                                     textAlignment:NSTextAlignmentRight];
    self.numLable.right = UIScreenWidth - 10;
    [whiteView addSubview:self.numLable];
    
    
    self.baoyouLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 80, 20)
                                                 text:@"不包邮"
                                            textColor:SecendTextColor
                                                 font:14
                                          isSizetoFit:NO
                                        textAlignment:NSTextAlignmentRight];;
    self.baoyouLable.right = UIScreenWidth - 15;;
    self.baoyouLable.bottom = self.numLable.top;
    [whiteView addSubview:self.baoyouLable];
    whiteView.height = self.numLable.st_bottom + 10;
    
    //第二个部分 促销取消
    //    self.cuxiaoView = [[UIView alloc] initWithFrame:CGRectMake(0, whiteView.st_bottom + 10, UIScreenWidth, 50)];
    //    self.cuxiaoView.backgroundColor = [UIColor whiteColor];
    //    [grayView addSubview:self.cuxiaoView];
    //    STLabel * cuxiaoLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 0, 40, 50)
    //                                                      text:@"促销"
    //                                                 textColor:GH_YellowBackGroundColor
    //                                                      font:15
    //                                               isSizetoFit:NO
    //                                             textAlignment:NSTextAlignmentLeft];
    //    [self.cuxiaoView addSubview:cuxiaoLable];
    //    self.cuxiaoLable = [[STLabel alloc] initWithFrame:CGRectMake(10 + cuxiaoLable.st_right, 0, UIScreenWidth - 20 - cuxiaoLable.st_right , 50)
    //                                                 text:@"满2752元，送价值999的水券一张"
    //                                            textColor:GH_secendTextColor
    //                                                 font:13
    //                                          isSizetoFit:NO
    //                                        textAlignment:NSTextAlignmentLeft];
    //    self.cuxiaoLable.numberOfLines = 0;
    //    [self.cuxiaoView addSubview:self.cuxiaoLable];
    //    [self.cuxiaoView st_showBottomShadow];
    
    //无数据view
    self.bootomView = [[UIView alloc] initWithFrame:CGRectMake(0, whiteView.st_bottom  +10, UIScreenWidth, 100)];
    self.bootomView.backgroundColor = [UIColor whiteColor];
    [grayView addSubview:self.bootomView];
    
    //请选择产品规格及数量
    __weak typeof(self) weakSelf =  self;
    STButton * typeButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 50)
                                                      title:@"商品详情"
                                                 titleColor:KL_firstTextColor
                                                  titleFont:14
                                               cornerRadius:0
                                            backgroundColor:[UIColor whiteColor]
                                            backgroundImage:nil
                                                      image:[UIImage imageNamed:@"我的收藏"]];
    typeButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    typeButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    [typeButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedGoodDetailButton:sender];
    }];
    [self.bootomView addSubview:typeButton];
    
    self.ziyingLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)
                                                     text:@"自营"
                                                textColor:[UIColor whiteColor]
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    self.ziyingLable.right = UIScreenWidth - 15;
    self.ziyingLable.centerY = typeButton.centerY;
    self.ziyingLable.backgroundColor = KL_BlueBackGroundColor;
    self.ziyingLable.layer.cornerRadius = 4;
    self.ziyingLable.clipsToBounds = YES;
    [self.bootomView addSubview:self.ziyingLable];
    //灰色
    UIView * bootomGrayView = [[UIView alloc] initWithFrame:CGRectMake(0, typeButton.st_bottom, UIScreenWidth, 10)];
    bootomGrayView.backgroundColor = KL_backgroundColor;
    [self.bootomView addSubview:bootomGrayView];
    
    self.iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, bootomGrayView.bottom + 10, 80, 80)];
    self.iconImageView.image = [UIImage imageNamed:@"06"];
    [self.bootomView addSubview:self.iconImageView];
    
    self.uploadLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 35 + bootomGrayView.bottom, 80, 14)
                                                 text:@"已缴纳保证金"
                                            textColor:KL_secendTextColor
                                                 font:12
                                          isSizetoFit:NO
                                        textAlignment:NSTextAlignmentRight];
    self.uploadLable.right = UIScreenWidth - 10;
   // [self.bootomView addSubview:self.uploadLable];
    UIImageView * shopImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    shopImageView.image = [UIImage imageNamed:@"icon_诚信"];
    shopImageView.centerX = self.uploadLable.centerX;
    shopImageView.bottom = self.uploadLable.top;
    [self.bootomView addSubview:shopImageView];
    
    STButton * contactButton = [[STButton alloc] initWithFrame:CGRectMake(0, self.uploadLable.bottom + 15, 70, 30)
                                                         title:@"联系商家"
                                                    titleColor:[UIColor whiteColor]
                                                     titleFont:14
                                                  cornerRadius:5
                                               backgroundColor:KL_BlueBackGroundColor
                                               backgroundImage:nil
                                                         image:nil];
    contactButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    contactButton.right = self.uploadLable.right;
    [contactButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedContactButton:sender];
    }];
    [self.bootomView addSubview:contactButton];
    
    STLabel * nameLable = [[STLabel alloc] initWithFrame:CGRectMake(10 + _iconImageView.right, _iconImageView.top + 3, self.uploadLable.left - 20 - self.iconImageView.right, 15)
                                                    text:@"猕猴桃"
                                               textColor:KL_firstTextColor
                                                    font:15
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentLeft];
    self.nameLable = nameLable;
    [self.bootomView addSubview:nameLable];
    
    self.xingxingView = [[STXingXIngView alloc] initWithFrame:CGRectMake(self.iconImageView.right  +10, self.nameLable.bottom + 5, self.uploadLable.left - 20 - self.iconImageView.right, 30) maxsNum:5];
    [self.xingxingView makeXingXingSelectedWithNum:2];
    [self.bootomView addSubview:self.xingxingView];
    
    self.idLable = [[STLabel alloc] initWithFrame:CGRectMake(self.iconImageView.right  +10, self.xingxingView.bottom, self.uploadLable.left - 20 - self.iconImageView.right, 40)
                                             text:@"匀加速ID"
                                        textColor:KL_secendTextColor
                                             font:13
                                      isSizetoFit:NO
                                    textAlignment:NSTextAlignmentLeft];
    [self.bootomView addSubview:self.idLable];
    
    self.goodDetailLable = [[STLabel alloc] initWithFrame:CGRectMake(10, self.iconImageView.bottom + 20, UIScreenWidth - 20, 40)
                                                     text:@"米好头等哈就是看会多久啊开始登记卡上登记卡上的安徽快点撒即可很多贾克斯ad哈贾克斯的黄金卡上登记卡上的卡萨骄傲的快速的黄金卡上的健康安徽登记卡还是登记卡上的熬枯受淡哈贾克斯的黄金卡上登记卡上的杰卡斯as快点哈烧烤活动"
                                                textColor:UIColorFromRGBA(0x686868)
                                                     font:13
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    self.goodDetailLable.height = [self.goodDetailLable.text st_heigthWithwidth:UIScreenWidth - 20 font:13] + 10;
    [self.bootomView addSubview:self.goodDetailLable];
    self.bootomView.height = self.goodDetailLable.bottom ;
    grayView.st_height = self.bootomView.bottom + 10;
    
    self.graView = grayView;
    self.st_height = grayView.bottom;
    
}

#pragma mark --Action Method
- (void)onSelectedGoodDetailButton:(UIButton*)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(onSelectedGoodDetailButton)]) {
        [self.delegate onSelectedGoodDetailButton];
    }
    
}
- (void)onSelectedContactButton:(UIButton*)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(onSelectedContactButton)]) {
        [self.delegate onSelectedContactButton];
    }
}
- (void)setDetailModel:(KLGoodDetailModel *)detailModel{
    _detailModel = detailModel;
    self.titleLable.text = detailModel.goods_name;
    if (detailModel.sale_type.integerValue == 1) {
        //价格出售
        self.priceLable.text = detailModel.price;
        if (detailModel.is_bargain) {
            //价格和便宜价格
            CGFloat  nowMoney = detailModel.bargain_price.floatValue  * [detailModel.price floatValue] * 0.01;
            self.priceLable.textColor  = KL_secendTextColor;
            self.priceLable.text = [NSString stringWithFormat:@"￥%.2f  原价:￥%@",nowMoney,detailModel.price];
            self.priceLable.text = [NSString stringWithFormat:@"￥%@",detailModel.price];
            if (detailModel.discount_price.length) {
                self.priceLable.text = [NSString stringWithFormat:@"￥%@  原价:￥%@",detailModel.discount_price,detailModel.price];
                NSString * originPrice = [NSString stringWithFormat:@"原价:￥%@",detailModel.price];
                NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle],NSForegroundColorAttributeName:KL_redColor};
                self.priceLable.attributedText = [self.priceLable.text st_convertAttributeStringWithKeyWord:originPrice attributes:attribtDic];
            }
        }
    }else{
         self.priceLable.text = @"折扣券";
    }
    
    self.numLable.text =  [NSString stringWithFormat:@"已售出:%@    库存:%@",
                           detailModel.sell_num.description,
                           detailModel.stock.description];
    
    //商品model
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:detailModel.shops.head_portrait] completed:nil];
    
    self.nameLable.text = detailModel.shops.shops_name;
    self.xingxingView.userInteractionEnabled = NO;
    [self.xingxingView makeXingXingSelectedWithNum:detailModel.shops.star.integerValue];
    
    self.idLable.text = [NSString stringWithFormat:@"匀加速ID:%@",detailModel.shops.gategoryID.description];
    
    self.goodDetailLable.text = detailModel.shops.abstract;
    
    [NSString lableAutoAdjustheightWithLabel:self.goodDetailLable];

    self.height = self.height - 40 + self.goodDetailLable.height ;
    
    
    if (self.detailModel.is_bao) {
        self.baoyouLable.text = @"包邮";
    }else{
        self.baoyouLable.text = @"不包邮";
    }
    
    if (self.detailModel.is_my) {
        self.ziyingLable.hidden = NO;
    }else{
        self.ziyingLable.hidden = YES;
    }
}

@end

